class BoardFilesController < ApplicationController
  def index
    list
    render :action => 'list'
  end

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }

  def list
    @board_file_pages, @board_files = 
		paginate :board_files, 
			:conditions =>['board_post_id = ?',params[:board_post_id]], 	
			:per_page => 10

  end

  def show
    @board_file = BoardFile.find(params[:id])
  end
 
  def dn
    @board_file = BoardFile.find(params[:id])
    send_data(@board_file.get_file_content,
              :filename => @board_file.file_name,
              :type => @board_file.mime_type,
              :disposition => "inline")
  end

  def new
    boardPost = BoardPost.find_by_id(params[:board_post_id])
    if(boardPost.user_id == session[:user_id])
    	@board_file = BoardFile.new
    else
	@flash[:notice] = "not user id"
    end
  end

  def create
    boardPost = BoardPost.find_by_id(params[:board_post_id])
    if(boardPost.user_id == session[:user_id])
     @board_file = BoardFile.new(params[:file])
     @board_file[:board_post_id] = params[:board_post_id]
     @board_file[:register_date] = Time.new.strftime("%Y%m%d") 
     @board_file.save

     responds_to_parent do
        render :update do |page|
            page["upload_msg"].innerHTML = "File Uploaded!"
            page.replace_html 'files', :partial => 'list'
        end
      end
    else
     responds_to_parent do
        render :update do |page|
            page["upload_msg"].innerHTML = "File Not Uploaded!"
            page.replace_html 'files', :partial => 'list'
        end
      end
    end

  end

  def edit
    @board_file = BoardFile.find(params[:id])
  end

  def update
    @board_file = BoardFile.find(params[:id])
    if @board_file.update_attributes(params[:board_file])
      flash[:notice] = 'BoardFile was successfully updated.'
      redirect_to :action => 'show', :id => @board_file
    else
      render :action => 'edit'
    end
  end

  def destroy
    boardPost = BoardPost.find_by_id(params[:board_post_id])
    if(boardPost.user_id == session[:user_id])
        BoardFile.find(params[:id]).destroy
#    redirect_to :action => 'list'
    end
  end
end
